package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-22
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    /**
     * 课程列表  基本实现
     * @return
     */
    //TODO 完善条件查询带分页
    @GetMapping
    public R getCourseList(){
         //来自 public interface IService<T>
        List<EduCourse> list = eduCourseService.list(null);

        return R.ok().data("list",list);


    }
    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

         //返回添加之后课程id,为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
         return R.ok().data("courseId",id);
    }

    //根据课程查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable  String courseId){
        CourseInfoVo  courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@PathVariable CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCOurseInfo(@PathVariable String id){

        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable  String id){

        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");//设置课程发布状态

        eduCourseService.updateById(course);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
         eduCourseService.removeCourse(courseId);
         return R.ok();
    }


}

